var verify = require('../../../../../../utils/verify-token.js')
var app = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		vendingData: [],
		index: 0,
		selectedDeputy: []
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		var deviceId = options.deviceid;
		var productId = options.productid;
		//console.log(options)
		this.setData({
			sh: app.globalData.screenHeight,
			sw: app.globalData.screenWidth,
			deviceId: deviceId,
			productId: productId
		})
		this.getVendingList();


	},
	bindPickerChange: function(e) {
		console.log(e);
		let that = this;
		let index = e.currentTarget.dataset.index;
		let idx = e.detail.value;
		that.data.selectedDeputy[index].forEach(item => {
			item.selected = false;
		})
		that.data.selectedDeputy[index][idx].selected = true;
		let selectedDeputy = that.data.selectedDeputy;
		that.setData({
			selectedDeputy: selectedDeputy,
			// vendingData:vendingData
		})
		//console.log(selectedDeputy);
	},
	//点击添加
	add() {
		let that = this;
		let selectedDeputy = that.data.selectedDeputy;
		let temp = that.data.vendingData;
		let list = [];
		temp.forEach(item => {
			let obj = {};
			for (let key in item) {
				obj[key] = item[key];
				obj['selected'] = false;
			}
			list.push(obj);
		})
		list[0] ? list[0].selected = true : '';
		selectedDeputy.push(list);
		that.setData({
			selectedDeputy: selectedDeputy
		})
		// console.log(selectedDeputy);
	},
	//点击删除
	delete(res) {
		let that = this;
		let selectedDeputy = that.data.selectedDeputy;
		let index = res.currentTarget.dataset.i;
		selectedDeputy.splice(index, 1);
		that.setData({
			selectedDeputy: selectedDeputy
		});
		// console.log(selectedDeputy);

	},
	//获取货机列表
	getVendingList() {
		var that = this;
		var baseUrl = app.globalData.baseUrl;
		var token = app.globalData.token;
		var productId = that.data.productId;
		wx.request({
			url: baseUrl + 'product/' + productId + '/vending/list',
			header: {
				'AUTHORIZATION': token,
				'content-type': 'application/x-www-form-urlencoded'
			},
			data: {
				vendingType: 'DEPUTY'
			},
			success(res) {
				if (res.data.code == 60006) {
					verify.clearAll()
				} else if (res.data.code == 200) {
					var vendingData = res.data.data;
					that.setData({
						vendingData: vendingData,
					})
					that.getDeputies();
				} else {
					wx.showModal({
						title: '操作失败',
						content: res.data.message,
					})
				}

			}
		})
	},
	//获取设备货机信息
	getDeputies() {
		var that = this;
		var baseUrl = app.globalData.baseUrl;
		var token = app.globalData.token;
		var deviceId = that.data.deviceId;
		wx.request({
			url: baseUrl + 'vending/device/' + deviceId + '/deputy',
			header: {
				'AUTHORIZATION': token,
				'content-type': 'application/x-www-form-urlencoded'
			},
			success(res) {
				if (res.data.code == 60006) {
					verify.clearAll()
				} else if (res.data.code == 200) {
					var selectedDeputy = res.data.data;
					var arr = [];
					that.data.vendingData.forEach(item => {
						for (let itm of selectedDeputy) {
							if (item.vendingId == itm.vendingId) {
								let obj = {
									count: 0,
									vendingName: '',
									vendingType: '',
									remark: '',
									vendingId: ''
								};
								item.count = itm.count;
								obj.count = item.count;
								obj.vendingName = item.vendingName;
								obj.vendingType = item.vendingType;
								obj.remark = item.remark;
								obj.vendingId = item.vendingId;
								arr.push(obj);
							}
						}
					})
					let brr = [];
					arr.forEach(item => {
						let crr = [];
						let temp = that.data.vendingData;
						for (let i = 0; i < temp.length; i++) {
							let tobj = {};
							for (let key in temp[i]) {
								tobj[key] = temp[i][key];
								tobj['selected'] = false;
								if (temp[i].vendingId == item.vendingId) {
									tobj['selected'] = true;
								}
							}
							crr.push(tobj);
						}

						brr.push(crr);
					})
					// console.log(brr);
					that.setData({
						selectedDeputy: brr
					})
				} else {
					wx.showModal({
						title: '操作失败',
						content: res.data.message,
					})
				}
			}
		})
	},

	setDeputy() {
		let that = this;
		let selectedDeputy = that.data.selectedDeputy;
		let arr = [];
		selectedDeputy.forEach(itm => {
			for (let obj of itm) {
				if (obj.selected) {
					arr.push(obj);
				}
			}
		});
		let vendingDeviceDtos = [];
		arr.forEach(item => {
			let obj = {};
			obj['count'] = 1;
			obj["vendingId"] = item.vendingId;
			vendingDeviceDtos.unshift(obj);
		})

		let baseUrl = app.globalData.baseUrl;
		let token = app.globalData.token;
		let deviceId = that.data.deviceId;
		wx.request({
			url: baseUrl + 'vending/device/' + deviceId + '/deputy/update',
			header: {
				'AUTHORIZATION': token,
			},
			data: {
				vendingDeviceDtos: vendingDeviceDtos
			},
			method: 'put',
			success(res) {
				if (res.data.code == 60006) {
					verify.clearAll()
				} else if (res.data.code == 200) {
					wx.showToast({
						title: '设置成功',
						icon: 'success',
						duration: 2000
					})
					that.getVendingList();
				} else {
					wx.showModal({
						title: '操作失败',
						content: res.data.message,
					})

				}
			}
		})

	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
